package com.example.myapplication2;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.view.Window;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Fragment weixinfragment=new weixinfragment();
    private Fragment configWeixinfragment=new config_weixinfragment();
    private Fragment contactWeixinfragment=new contact_weixinfragment();
    private Fragment friendsWeixinfragment=new friends_weixinfragment();

    private FragmentManager fragmentManager;
    private LinearLayout linearLayout1,linearLayout2,linearLayout3,linearLayout4;
    private ImageView imageView1,imageView2,imageView3,imageView4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main2);

        linearLayout1=findViewById(R.id.linearlayout_weixin);
        linearLayout2=findViewById(R.id.linearlayout_config);
        linearLayout3=findViewById(R.id.linearlayout_contact);
        linearLayout4=findViewById(R.id.linearlayout_friend);

        imageView1=findViewById(R.id.imageView1);
        imageView2=findViewById(R.id.imageView2);
        imageView3=findViewById(R.id.imageView3);
        imageView4=findViewById(R.id.imageView4);

        linearLayout1.setOnClickListener(this);
        linearLayout2.setOnClickListener(this);
        linearLayout3.setOnClickListener(this);
        linearLayout4.setOnClickListener(this);

        initFragment();

    }
    private void initFragment(){
        fragmentManager=getFragmentManager();
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        transaction.add(R.id.id_content,weixinfragment);
        transaction.add(R.id.id_content,contactWeixinfragment);
        transaction.add(R.id.id_content,configWeixinfragment);
        transaction.add(R.id.id_content,friendsWeixinfragment);
        hideFragment(transaction);
        transaction.commit();
    }
    private void hideFragment( FragmentTransaction transaction){

        transaction.hide(weixinfragment);
        transaction.hide(contactWeixinfragment);
        transaction.hide(configWeixinfragment);
        transaction.hide(friendsWeixinfragment);
    }
    private void showfragment(int i){
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        hideFragment(transaction);
        switch (i){
            case 0:
                transaction.show(weixinfragment);
                break;
            case 1:
                transaction.show(configWeixinfragment);
                break;
            case 2:
                transaction.show(contactWeixinfragment);
                break;
            case 3:
                transaction.show(friendsWeixinfragment);
                break;
            default:
                break;

        }
        transaction.commit();
    }
    public void reImage(int i)
    {
        imageView1.setImageResource(R.drawable.tab_weixin_normal);
        imageView2.setImageResource(R.drawable.tab_find_frd_normal);
        imageView3.setImageResource(R.drawable.tab_address_normal);
        imageView4.setImageResource(R.drawable.tab_settings_normal);
        switch (i)
        {
            case 0:
                imageView1.setImageResource((R.drawable.tab_weixin_pressed));
                break;
            case 1:
                imageView2.setImageResource(R.drawable.tab_find_frd_pressed);
                break;
            case 2:
                imageView3.setImageResource(R.drawable.tab_address_pressed);
                break;
            case 3:
                imageView4.setImageResource(R.drawable.tab_settings_pressed);
                break;
            default:
                break;
        }
    }
    @Override
    public void onClick(View v){
        switch (v.getId()){
            case R.id.linearlayout_weixin:
                reImage(0);
                showfragment(0);
                break;
            case R.id.linearlayout_config:
                reImage(3);
                showfragment(1);
                break;
            case R.id.linearlayout_contact:
                reImage(2);
                showfragment(2);
                break;
            case R.id.linearlayout_friend:
                reImage(1);
                showfragment(3);
                break;
            default:
                break;


        }
    }
}